package com.atguigu.atcrowdfunding.manager.controller;

import com.atguigu.atcrowdfunding.bean.Role;
import com.atguigu.atcrowdfunding.bean.User;
import com.atguigu.atcrowdfunding.manager.service.UserService;
import com.atguigu.atcrowdfunding.util.AjaxResult;
import com.atguigu.atcrowdfunding.util.Page;
import com.atguigu.atcrowdfunding.util.StringUtil;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author xzt
 * @create 2021-02-14 15:12
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String toIndex(){
        return "user/index";
    }

    //异步请求
    @ResponseBody
    @RequestMapping("/doIndex")
    public Object index(@RequestParam(value = "pageNo",defaultValue = "1",required = false) Integer pageNo,
                        @RequestParam(value = "pageSize",defaultValue = "10",required = false) Integer pageSize,
                        String queryText){
        AjaxResult ajaxResult = new AjaxResult();
        try {

            Map<String,Object> paramMap = new HashMap();

            paramMap.put("pageNo",pageNo);
            paramMap.put("pageSize",pageSize);

            if(StringUtil.isNotEmpty(queryText)){

                if(queryText.contains("%")){
                    queryText = queryText.replaceAll("%","\\\\%");
                }

                paramMap.put("queryText",queryText);
            }

            Page page = userService.queryPage(paramMap);

            ajaxResult.setSuccess(true);
            ajaxResult.setPage(page);
            ajaxResult.setMessage("查询数据成功");
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("查询数据失败");
            e.printStackTrace();
        }


        return ajaxResult;
    }

    //同步请求
    /*@RequestMapping("/index")
    public String index(@RequestParam(value = "pageNo",defaultValue = "1",required = false) Integer pageNo, @RequestParam(value = "pageSize",defaultValue = "10",required = false) Integer pageSize, Model model){

        Page page = userService.queryPage(pageNo,pageSize);

        model.addAttribute("page", page);

        return "user/index";
    }*/

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "user/add";
    }

    @ResponseBody
    @RequestMapping("/doAdd")
    public Object doAdd(User user){

        AjaxResult ajaxResult = new AjaxResult();
        try {
            int count = userService.saveUser(user);
            if(count == 1){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("添加数据成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("添加数据失败");
            }
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("添加数据失败");
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String loginacct, Model model){
        System.out.println(loginacct);
        User user = userService.getUserByLoginacct(loginacct);

        System.out.println(user);

        model.addAttribute("user",user);
        return "user/update";
    }

    @ResponseBody
    @RequestMapping("/doUpdate")
    public Object doUpdate(User user){

        AjaxResult ajaxResult = new AjaxResult();
        try {

            int count = userService.updateUser(user);

            if(count == 1){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("修改数据成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("修改数据失败");
            }
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("修改数据失败");
            e.printStackTrace();
        }

        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/doDelete")
    public Object doDelete(String loginacct){
        AjaxResult ajaxResult = new AjaxResult();
        try {

            int count = userService.deleteUser(loginacct);

            if(count == 1){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("删除数据成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("删除数据失败");
            }
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("删除数据失败");
            e.printStackTrace();
        }

        return ajaxResult;
    }


    @ResponseBody
    @RequestMapping("/doDeleteAll")
    public Object doDeleteAll(@RequestParam(value = "ids[]") int[] ids){
        AjaxResult ajaxResult = new AjaxResult();
        try {

            int count = userService.deleteUsers(ids);

            if(count == ids.length){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("批量删除数据成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("批量删除数据失败");
            }
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("批量删除数据失败");
            e.printStackTrace();
        }

        return ajaxResult;
    }

    @RequestMapping("/assignRole")
    public String assignRole(Integer id,Model model){

        List<Role> allList = userService.queryAllRole();

        List<Integer> roleIds = userService.queryRoleByUserId(id);

        List<Role> leftRoleList = new ArrayList<>();//未分配角色
        List<Role> rightRoleList = new ArrayList<>();//已分配角色

        for (Role role : allList) {
            if(roleIds.contains(role.getId())){
                rightRoleList.add(role);
            }else{
                leftRoleList.add(role);
            }
        }
        model.addAttribute("leftRoleList",leftRoleList);
        model.addAttribute("rightRoleList",rightRoleList);


        return "user/assignRole";
    }

    @ResponseBody
    @RequestMapping("/doAssignRole")
    public Object doAssignRole(@RequestParam("userid") Integer userid, @RequestParam("roleid[]") Integer[] roleid){
        AjaxResult ajaxResult = new AjaxResult();
        try {

            Integer count = userService.addRoleById(userid,roleid);

            if(count == 1){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("分配成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("分配失败");
            }
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("分配失败");
            e.printStackTrace();
        }

        return ajaxResult;

    }


    @ResponseBody
    @RequestMapping("/doUnAssignRole")
    public Object doUnAssignRole(@RequestParam("userid") Integer userid, @RequestParam("roleid[]") Integer[] roleid){
        AjaxResult ajaxResult = new AjaxResult();
        try {

            userService.removeRoleById(userid,roleid);


            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("取消分配成功");

        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("取消分配失败");
            e.printStackTrace();
        }

        return ajaxResult;

    }

}
